/*** Eclipse Class Decompiler plugin, copyright (c) 2016 Chen Chao (cnfree2000@hotmail.com) ***/
package nc.bs.mmpac.reqpickm.transfer.warehouse;

import java.util.List;

import com.google.gson.JsonObject;

import nc.bs.dao.BaseDAO;
import nc.bs.mmpac.reqpickm.transfer.pubhandler.IWorkTaskHandler;
import nc.bs.mmpac.reqpickm.transfer.util.DetailInfoQueryUtils;
import nc.jdbc.framework.processor.ArrayListProcessor;
import nc.ms.tb.oba.test.JSonEncoderTester;
import nc.pub.templet.converter.util.helper.ExceptionUtils;
import nc.util.mmf.framework.db.MMSqlBuilder;
import nc.vo.pub.BusinessException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class GetTransBillInfoHandler implements IWorkTaskHandler {
	public JSONObject doAction(JSONObject cond) throws BusinessException {
		String transBillCode = cond.getString("transBillCode");
		String rowno = cond.getString("rowno");
		if ((transBillCode != null) && (rowno != null)) {
			MMSqlBuilder sql = new MMSqlBuilder();
			sql.append("select h.vbillcode,b.crowno,b.cmaterialvid,b.nnum from ic_whstrans_h h,ic_whstrans_b b where h.dr=0 and b.dr=0 and h.cspecialhid=b.cspecialhid and ");
			sql.append("h.vbillcode", transBillCode);
			sql.and();
			sql.append("b.crowno", rowno);
			BaseDAO dao = new BaseDAO();
			try {
				List rstList = (List) dao.executeQuery(sql.toString(),
						new ArrayListProcessor());
				if ((rstList == null) || (rstList.size() <= 0))
					return null;
				JSONObject result = new JSONObject();
				Object[] objs = (Object[]) rstList.get(0);
				result.put("vbillcode", objs[0]);
				result.put("rowno", objs[1]);
				String cmaterialvid = (String) objs[2];
				if (cmaterialvid != null) {
					JSONArray matinfos = DetailInfoQueryUtils
							.queryMatInfoByPks(new String[] { cmaterialvid });
					if ((matinfos != null) && (matinfos.size() > 0)) {
						result.put("matinfo", matinfos.get(0));
					}
				}
				result.put("nnum", objs[3]);
				MMSqlBuilder sql_b = new MMSqlBuilder();
				sql_b.append("select vbatchcode,pk_batchcode from scm_batchcode where cmaterialoid = '"+cmaterialvid+"'");
				List batList = (List)dao.executeQuery(sql_b.toString(), new ArrayListProcessor());
				JSONArray array = new JSONArray();
				for(Object obj:batList){
					Object[] batObjs = (Object[])obj;
					JSONObject json = new JSONObject();
					json.put("pk_batchcode",batObjs[1]);
					json.put("batchcode", batObjs[0]);
					array.add(json);
				}
				result.put("batchcode",array);
				return result;
			} catch (Exception e) {
				ExceptionUtils.wrapException(e);
			}
		}
		return null;
	}
}